﻿using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Dto.WorkingCenter;
using SHKF.B2C.Model.Entity.WorkingCenter;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SHKF.B2C.Mannage.AdminUI.View.CenterInfo.Product
{
    /// <summary>
    /// frmSearchProductRelated.xaml 的交互逻辑
    /// </summary>
    public partial class frmSearchProductRelated : Window
    {
        public frmSearchProductRelated()
        {
            InitializeComponent();
        }

        public long ProductVSN = 0; //虚拟产品SN
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDataWareHouse();
        }
         
        private async void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if(this.dgvDataWarehous.SelectedValue == null)
            {
                MessageBox.Show("请选择分仓");
                return;
            }

            if (this.dgvProduct.SelectedValue == null)
            {
                MessageBox.Show("请选择产品");
                return;
            }

            ProductInfoRelatedEntity info = new ProductInfoRelatedEntity();

            DataWarehousInfoEntity data = (DataWarehousInfoEntity)this.dgvDataWarehous.SelectedValue;
            info.WarehousSN = data.WarehousSN;
            info.WarehousName = data.WarehousName;
            info.ProductSN = ((ErpProductInfo)this.dgvProduct.SelectedValue).SN;
            info.ProductVSN = ProductVSN; 
            ProductInfoRelatedDto dto = await GL.HttpClientManage.SaveProductInfoRelated(info);
            if( dto.error == null)
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }

        private async void btnSearchProduct_Click(object sender, RoutedEventArgs e)
        {
            if( this.dgvDataWarehous.SelectedValue != null)
            {
                this.lblProductCount.Content = 0;
                DataWarehousInfoEntity warehous = this.dgvDataWarehous.SelectedValue as DataWarehousInfoEntity;
                ErpProductInfoListDto dto = await GL.HttpClientManage.QueryProductErpList(warehous.ServerName, warehous.WarehousSN, this.txtProduct.Text);
                if (dto.error == null)
                {
                    this.dgvProduct.ItemsSource = dto.data;
                    this.lblProductCount.Content = dto.data.Count;
                }
                else
                {
                    MessageBox.Show(dto.error.msg);
                }
            } 
        }

        private void btnFindDataWarehous_Click(object sender, RoutedEventArgs e)
        {
            LoadDataWareHouse();
        }

        //加载分仓信息
        private async void LoadDataWareHouse()
        {
            DataWarehousInfoListDto dto = await GL.HttpClientManage.QueryDataWarehousInfoList(this.txtKeyWord.Text);
            if (dto.error == null)
            {
                this.dgvDataWarehous.ItemsSource = dto.data;
            }
            else
            {
                MessageBox.Show(dto.error.msg);
            }
        }
    }
}
